import java.io.*;
import java.util.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;




public class Download {

public static String userName = null;
public static String[] result=new String [500];
public static String DayNow = null;


public static void input_handle() {

      System.out.print("Please type the assignment you wish to download : ");


      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("Error while downloading your assignment, please try again!");
         input_handle();
      }


 	int idx=0; int tokenCount;



	 result = userName.split(",");






}





  public static void main(String[] args) throws IOException {





     System.out.println("following are the available assignments to download");
     System.out.println(" ");
     File file = new File("C:\\Agile\\Assignment Sheet\\assignment list.txt");
     FileInputStream fis = null;
     BufferedInputStream bis = null;
     DataInputStream dis = null;

    try {
           fis = new FileInputStream(file);
      // Here BufferedInputStream is added for fast reading.
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);
      // dis.available() returns 0 if the file does not have more lines.
      while (dis.available() != 0) {

      // this statement reads the line from the file and print it to
        // the console.
        System.out.println(dis.readLine());
      }

      // dispose all the resources after using them.
      fis.close();
      bis.close();
      dis.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }


     System.out.println("");

    input_handle();


    File inputFile = new File("Assignment Sheet\\"+result[0]+".txt");
    File outputFile = new File("Downloads\\"+result[0]+".txt");



    FileReader in = new FileReader(inputFile);


    FileWriter out = new FileWriter(outputFile);
    int c;

    while ((c = in.read()) != -1)
    out.write(c);

    in.close();
    out.close();


	System.out.println("");
        System.out.println("Assignment succesfully downloaded to Downloads folder");








  }



}
